package cn.kgc.thirdtest.controller;

import cn.kgc.thirdtest.pojo.Itemtypes;
import cn.kgc.thirdtest.pojo.Peitems;
import cn.kgc.thirdtest.service.ItemtypeService;
import cn.kgc.thirdtest.service.PeitemsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/doManage")
public class ManageController {
    @Resource
    private ItemtypeService itemtypeService;
    @Resource
    private PeitemsService peitemsService;

    //获取全部项目列表
    @RequestMapping("/getItemList")
    public String getItemList(Model model) {
        List<Itemtypes> itemtypeList = null;
        itemtypeList = itemtypeService.getItemtypesList();
        model.addAttribute("itemtypeList", itemtypeList);
        return "main";
    }

    //根据大项目查询具体信息小项目
    @RequestMapping("/getPeitemsList")
    public String getPeitemList(@RequestParam(value = "queryTypeid") String queryTypeid,
                                Model model) {
        List<Peitems> peitemsList = peitemsService.getPeitemListByTypeid(queryTypeid);
        List<Itemtypes> itemtypeList = itemtypeService.getItemtypesList();
        model.addAttribute("itemtypeList", itemtypeList);
        model.addAttribute("peitemsList", peitemsList);
        return "main";
    }
    //点击修改后，跳转至修改界面
    @RequestMapping("/toModifyView")
    public  String toModifyView(@RequestParam("itemid")String itemid,
                                Model model){
        Peitems peitems=peitemsService.getPeitemByItemid(itemid);
        model.addAttribute("peitems",peitems);
        return "modifypeitem";

    }
    //保存修改信息
    @RequestMapping("/saveModify")
    public  @ResponseBody String saveModify(@RequestParam("peitems") Peitems peitems){
        int i=peitemsService.updatePeitemById(peitems);
        Map<String,String> resultMap=new HashMap<>();
        if (i>0){
            resultMap.put("resultMap","true");
        }else {
            resultMap.put("resultMap","false");
        }
        return "";
    }
}
